<template>
    <div class="common">
        <el-form 
            :model="form" 
            :rules="rules" 
            ref="ruleForm" 
            label-width="100px" 
            class="myform">
            <el-form-item label="手机号码" prop="mobile">
                <el-input v-model="form.mobile"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" prop="password">
                <el-input  type="password" show-password v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="滑块验证" prop="slide">
                <Slide v-model="form.slide" :successFun="onsuccessFun" :errorFun="onerrorFun" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" class="width100" @click="loginTodo" >立即登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {signinAjax} from "@/api"
import Slide from "@/components/Slide.vue"
export default {
    components:{
        Slide
    },
    data(){
        return {
            form:{
                mobile:""
            },
            rules:{   // 表单校验 
                mobile:[
                    {required:true,message:"请输入手机号码",trigger:'blur'},
                    {
                        validator:(rule, value, callback)=>{
                            var mobileReg = /^1(3|5|6|7|8|9)\d{9}$/
                            if(mobileReg.test(value)){
                                callback()
                            }else{
                                callback(new Error("请输入正确的手机号码"));
                            }
                        },
                        trigger:'change'
                    }
                ],
                password:[
                    {required:true,message:"请输入登录密码",trigger:'blur'},
                    {
                        validator:(rule, value, callback)=>{
                            var passReg = /^[A-Za-z0-9]{4,12}$/
                            if(value.length<4){
                                callback(new Error("密码长度至少4位"));
                            }else if(value.length>12){
                                callback(new Error("密码长度最多12位"));
                            }else if(!passReg.test(value)){
                                callback(new Error("请输入正确的密码"));
                                
                            }else{
                                callback()
                            }
                        },
                        trigger:'change'
                    }
                ],
                slide: [
                // { message: "请进行滑块验证", trigger: "blur" },
                {
                    validator: (rule, value, callback) => {
                    console.log(value)
                    if (!value) {
                        callback(new Error("验证失败"));
                    } else {
                        callback();
                    }
                    },
                    trigger: "blur",
                },
                ],
            }
        }
    },
    methods:{
        loginTodo(){
            console.log("sddss")
            this.$refs.ruleForm.validate(valid=>{  // 表单的全局校验 
                if(valid){
                    signinAjax(this.form)
                    .then(res=>{
                        if(res.type){
                            // 登录成功
                            sessionStorage.setItem("token",res.token);
                            localStorage.setItem("mobile",this.form.mobile);
                            this.$router.push({name:'index'})  // 登录成功进入主页 
                            
                        }
                    })
                    .catch(err=>{
                        
                    })
                }else{
                    console.log("error");
                    return false;
                }
            })
        },
        onsuccessFun() {
            this.form.slide = true;
            },
        onerrorFun() {},
    },
    mounted(){
        if(localStorage.getItem("mobile")){
            this.form.mobile = localStorage.getItem("mobile")
        }
    }
}
</script>

<style lang="scss" scoped>
.myform{
    padding-right:50px;
    padding-top:20px;
}
.width100{
    width:100%;
}
</style>